// VBConversions Note: VB project level imports
using System.Collections.Generic;
using System;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Xml.Linq;
using Microsoft.VisualBasic.Compatibility;
using System.Windows.Forms;
using System.Collections;
using System.Data;
// End of VB project level imports

using System.IO;


namespace 喷码数据生成工具
{
	public partial class frm_DHL
	{
		public frm_DHL()
		{
			InitializeComponent();
		}
		
		CommFunction.DHLDataStructure tmpData = new CommFunction.DHLDataStructure();
		public void frm_DHL_Load(System.Object sender, System.EventArgs e)
		{
			Label2.Text = "提示：" + "\r\n" + "  双击列表相应行数进行数据修改！";
			TB_SavePath.Text = Form1.Default.SaveFile.Text;
			ListView1.Items.Add("起始号码1"); //0
			ListView1.Items.Add("起始号码2"); //1
			ListView1.Items.Add("终止号码1"); //2
			ListView1.Items.Add("终止号码2"); //3
			ListView1.Items.Add("REGION"); //4
			ListView1.Items.Add("COUNTRY"); //5
			ListView1.Items.Add("REGION COORD"); //6
			ListView1.Items.Add("PRODUCT"); //7
			ListView1.Items.Add("PERIOD"); //8
			ListView1.Items.Add("PO NO"); //9
			ListView1.Items.Add("TO NO"); //10
			ListView1.Items.Add("装箱数"); //11
			ListView1.Items.Add("每盘总箱数"); //12
			ListView1.Items.Add("起始箱号"); //13
			ListView1.Items.Add("起始盘号"); //14
			CommFunction.SetItemValue(ListView1.Items[0], 1, CommFunction.SaveData.DHLData.StartNO);
			CommFunction.SetItemValue(ListView1.Items[1], 1, Form1.Default.InsertChar(CommFunction.SaveData.DHLData.StartNO, " "));
			CommFunction.SetItemValue(ListView1.Items[2], 1, CommFunction.SaveData.DHLData.EndNo);
			CommFunction.SetItemValue(ListView1.Items[3], 1, Form1.Default.InsertChar(CommFunction.SaveData.DHLData.EndNo, " "));
			CommFunction.SetItemValue(ListView1.Items[4], 1, CommFunction.SaveData.DHLData.Region);
			CommFunction.SetItemValue(ListView1.Items[5], 1, CommFunction.SaveData.DHLData.Country);
			CommFunction.SetItemValue(ListView1.Items[6], 1, CommFunction.SaveData.DHLData.RegionCoord);
			CommFunction.SetItemValue(ListView1.Items[7], 1, CommFunction.SaveData.DHLData.Product);
			CommFunction.SetItemValue(ListView1.Items[8], 1, CommFunction.SaveData.DHLData.Period);
			CommFunction.SetItemValue(ListView1.Items[9], 1, CommFunction.SaveData.DHLData.PO_NO);
			CommFunction.SetItemValue(ListView1.Items[10], 1, CommFunction.SaveData.DHLData.TO_NO);
			if (CommFunction.SaveData.DHLData.StartXH > 0)
			{
				CommFunction.SetItemValue(ListView1.Items[13], 1, CommFunction.SaveData.DHLData.StartXH);
			}
			else
			{
				CommFunction.SetItemValue(ListView1.Items[13], 1, "1");
			}
			if (CommFunction.SaveData.DHLData.StartPH > 0)
			{
				CommFunction.SetItemValue(ListView1.Items[14], 1, CommFunction.SaveData.DHLData.StartPH);
			}
			else
			{
				CommFunction.SetItemValue(ListView1.Items[14], 1, "1");
			}
			if (CommFunction.SaveData.DHLData.每箱号码数 > 0)
			{
				CommFunction.SetItemValue(ListView1.Items[11], 1, System.Convert.ToString(CommFunction.SaveData.DHLData.每箱号码数));
			}
			else
			{
				CommFunction.SetItemValue(ListView1.Items[11], 1, "1000");
			}
			if (CommFunction.SaveData.DHLData.每盘总箱数 > 0)
			{
				CommFunction.SetItemValue(ListView1.Items[12], 1, System.Convert.ToString(CommFunction.SaveData.DHLData.每盘总箱数));
			}
			else
			{
				CommFunction.SetItemValue(ListView1.Items[12], 1, "90");
			}
			CommFunction.ChangeButtonFromFrm(this);
		}
		
		public void TB_SavePath_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			//e.Handled = True
		}
		
		public void Btn_OpenSaveFile_Click(System.Object sender, System.EventArgs e)
		{
			SaveFileDialog NewDialog = new SaveFileDialog();
			NewDialog.Title = "保存";
			NewDialog.Filter = "文本文件(*.txt)|*.txt";
			NewDialog.FileName = "1";
			NewDialog.CheckPathExists = true;
			if (NewDialog.ShowDialog() == Windows.Forms.DialogResult.OK)
			{
				TB_SavePath.Text = NewDialog.FileName;
			}
		}
		
		public void ListView1_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			string Command = CommFunction.getItemValue(ListView1.SelectedItems[0], 0);
			ListViewItem lItem = ListView1.SelectedItems[0];
			string ReturnValue = Constants.vbNullString;
			if (Command == "起始号码1")
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入起始号码", CommFunction.getItemValue(ListView1.SelectedItems[0], 1));
				if (!string.IsNullOrEmpty(ReturnValue))
				{
					CommFunction.SetItemValue(ListView1.Items[1], 1, Form1.Default.InsertChar(ReturnValue, " "));
				}
			}
			else if (Command == ("起始号码2"))
			{
			}
			else if (Command == ("终止号码1"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入终止号码", CommFunction.getItemValue(ListView1.SelectedItems[0], 1));
				if (!string.IsNullOrEmpty(ReturnValue))
				{
					CommFunction.SetItemValue(ListView1.Items[3], 1, Form1.Default.InsertChar(ReturnValue, " "));
				}
			}
			else if (Command == ("终止号码2"))
			{
			}
			else if (Command == ("REGION"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入REGION", CommFunction.getItemValue(ListView1.SelectedItems[0], 1), false);
			}
			else if (Command == ("COUNTRY"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入COUNTRY", CommFunction.getItemValue(ListView1.SelectedItems[0], 1), false);
			}
			else if (Command == ("REGION COORD"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入REGION COORD", CommFunction.getItemValue(ListView1.SelectedItems[0], 1), false);
			}
			else if (Command == ("PRODUCT"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入PRODUCT", CommFunction.getItemValue(ListView1.SelectedItems[0], 1), false);
			}
			else if (Command == ("PERIOD"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入PERIOD", CommFunction.getItemValue(ListView1.SelectedItems[0], 1), false);
			}
			else if (Command == ("PO NO"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入PO NO", CommFunction.getItemValue(ListView1.SelectedItems[0], 1), false);
			}
			else if (Command == ("TO NO"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入TO NO", CommFunction.getItemValue(ListView1.SelectedItems[0], 1), false);
			}
			else if (Command == ("装箱数"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入装箱数", CommFunction.getItemValue(ListView1.SelectedItems[0], 1));
			}
			else if (Command == ("每盘总箱数"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入每盘总箱数", CommFunction.getItemValue(ListView1.SelectedItems[0], 1));
			}
			else if (Command == ("起始箱号"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入每盘总箱数", CommFunction.getItemValue(ListView1.SelectedItems[0], 1));
			}
			else if (Command == ("起始盘号"))
			{
				ReturnValue = RemedyCode.ShowInput(this, "输入每盘总箱数", CommFunction.getItemValue(ListView1.SelectedItems[0], 1));
			}
			if (!string.IsNullOrEmpty(ReturnValue))
			{
				CommFunction.SetItemValue(lItem, 1, ReturnValue);
			}
		}
		
		public void ListView1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			
		}
		
		public void Btn_CreateData_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				tmpData.StartNO = CommFunction.getItemValue(ListView1.Items[0], 1);
				tmpData.EndNo = CommFunction.getItemValue(ListView1.Items[2], 1);
				tmpData.Region = CommFunction.getItemValue(ListView1.Items[4], 1);
				tmpData.Country = CommFunction.getItemValue(ListView1.Items[5], 1);
				tmpData.RegionCoord = CommFunction.getItemValue(ListView1.Items[6], 1);
				tmpData.Product = CommFunction.getItemValue(ListView1.Items[7], 1);
				tmpData.Period = CommFunction.getItemValue(ListView1.Items[8], 1);
				tmpData.PO_NO = CommFunction.getItemValue(ListView1.Items[9], 1);
				tmpData.TO_NO = CommFunction.getItemValue(ListView1.Items[10], 1);
				tmpData.每箱号码数 = (int) (Conversion.Val(CommFunction.getItemValue(ListView1.Items[11], 1)));
				tmpData.每盘总箱数 = (int) (Conversion.Val(CommFunction.getItemValue(ListView1.Items[12], 1)));
				tmpData.StartXH = (long) (Conversion.Val(CommFunction.getItemValue(ListView1.Items[13], 1)));
				tmpData.StartPH = (long) (Conversion.Val(CommFunction.getItemValue(ListView1.Items[14], 1)));
				if (tmpData.StartNO >= tmpData.EndNo)
				{
					MessageBox.Show(this, "起始号码不能等于中止号码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				return ;
				FileStream fs = default(FileStream);
				if (File.Exists(TB_SavePath.Text))
				{
					if (CheckBox1.Checked == false)
					{
						if (MessageBox.Show(this, "文件已经存在是否覆盖？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == Windows.Forms.DialogResult.No)
						{
							return ;
						}
						else
						{
							fs = new FileStream(TB_SavePath.Text, FileMode.Create, FileAccess.Write);
						}
					}
					else
					{
						fs = new FileStream(TB_SavePath.Text, FileMode.Append, FileAccess.Write);
					}
				}
				else
				{
					fs = new FileStream(TB_SavePath.Text, FileMode.Create, FileAccess.Write);
				}
				StreamWriter fw = new StreamWriter(fs, System.Text.Encoding.Default);
				long XS = (long) ((double.Parse(tmpData.EndNo) - double.Parse(tmpData.StartNO) + 1) / tmpData.每箱号码数);
				int xp = System.Convert.ToInt32((double) XS / tmpData.每盘总箱数);
				NeroBar1.Maximum = XS;
				NeroBar1.Value = 0;
				for (var i = Conversion.Val(tmpData.StartNO); i <= Conversion.Val(tmpData.EndNo); i += tmpData.每箱号码数)
				{
					fw.WriteLine(i);
					fw.WriteLine(Form1.Default.InsertChar(System.Convert.ToString(i), " "));
					fw.WriteLine(i + tmpData.每箱号码数 - 1);
					fw.WriteLine(Form1.Default.InsertChar(System.Convert.ToString(i + tmpData.每箱号码数 - 1), " "));
					fw.WriteLine(CommFunction.FormatByLength(tmpData.StartPH, xp.ToString().Length));
					fw.WriteLine(CommFunction.FormatByLength(tmpData.StartXH, XS.ToString().Length));
					fw.WriteLine(tmpData.每箱号码数.ToString());
					fw.WriteLine(tmpData.Region);
					fw.WriteLine(tmpData.Country);
					fw.WriteLine(tmpData.RegionCoord);
					fw.WriteLine(tmpData.Product);
					fw.WriteLine(tmpData.Period);
					fw.WriteLine(tmpData.PO_NO);
					tmpData.StartXH++;
					if (System.Convert.ToInt32(tmpData.StartXH - 1) % tmpData.每盘总箱数 == 0)
					{
						tmpData.StartPH++;
					}
					NeroBar1.Value++;
					(new Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase()).DoEvents();
				}
				CommFunction.SetItemValue(ListView1.Items[13], 1, tmpData.StartXH);
				CommFunction.SetItemValue(ListView1.Items[14], 1, tmpData.StartPH);
				fw.Close();
				fs.Close();
				CommFunction.SaveData.DHLData = tmpData;
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
	}
}
